package com.sdk.bigdata.model;


import com.alibaba.fastjson.annotation.JSONField;
import com.sdk.bigdata.exceptions.DataException;

import java.util.HashMap;
import java.util.Map;

// 设备行为事件
public class Device extends BaseModel {
    private String event;

    @JSONField(name = "device_no")
    private String deviceNo;

    @JSONField(name = "device_type")
    private String deviceType;

    @JSONField(name = "device_name")
    private String deviceName;

    public Device(){}

    public Device(String type, String event, String deviceNo, String deviceType, String deviceName,Map<String, Object> properties) {
        super(type, properties);
        this.event = event;
        this.deviceNo = deviceNo;
        this.deviceType = deviceType;
        this.deviceName = deviceName;
    }

    public String getEvent() {
        return event;
    }

    public void setEvent(String event) {
        this.event = event;
    }

    public String getDeviceNo() {
        return deviceNo;
    }

    public void setDeviceNo(String deviceNo) {
        this.deviceNo = deviceNo;
    }

    public String getDeviceType() {
        return deviceType;
    }

    public void setDeviceType(String deviceType) {
        this.deviceType = deviceType;
    }

    public String getDeviceName() {
        return deviceName;
    }

    public void setDeviceName(String deviceName) {
        this.deviceName = deviceName;
    }


    @Override
    public void validAttribute() {
        this.setType("device");
        this.setTime(System.currentTimeMillis());

        if (null == this.getDeviceNo() || this.getDeviceNo().equals("")) {
            throw new DataException("property [DeviceNo] can't be empty");
        }
        if (this.getDeviceNo().length() > 255) {
            throw new DataException("the max length of property [DeviceNo] is 255");
        }
        if (!"imei,idfa,android_id,oaid,mac".contains(this.getDeviceType())) {
            throw new DataException("property [DeviceType] is not in imei,idfa,android_id,oaid,mac");
        }
        if (null == this.getDeviceName() || this.getDeviceName().equals("")) {
            throw new DataException("property [DeviceName] can't be empty");
        }

        if (null == this.getProperties()) {
            this.setProperties(new HashMap<>());
        }
    }
}
